//
//  GZNDepIpViewController.m
//  Cattle
//  Copyright (c) 2015年 xxcao. All rights reserved.
//
/*


- (IBAction)submitBtnClick:(id)sender {
    if([self.portTextField isFirstResponder]){
        [self.portTextField resignFirstResponder];
    }
    if([Common isEmptyString:self.portTextField.text]){
        [Common promptShowTextTips:@"请输入服务器端口" Time:2.0];
        return;
    }
    if(![Common isAllNum:self.portTextField.text]){
        [Common promptShowTextTips:@"非法端口号,请从新输入" Time:2.0];
        return;
    }
    if(self.portTextField.text.length > 5){
        [Common promptShowTextTips:@"端口号过长" Time:2.0];
        return;
    }
    
    NSString *protocolMark = nil;
    if ([Common string:self.addressStr ContainSubString:@"jobfreely"] ||
        [Common string:self.addressStr ContainSubString:@"cattlesaas"]) {
        protocolMark = PROTOCOL_HTTPS;
    } else {
        protocolMark = PROTOCOL_HTTP;
    }
    kBaseUrl = [NSString stringWithFormat:@"%@//%@:%@/MobileApi/",protocolMark,self.addressStr,self.portTextField.text];
    UserDefaultsSave(self.addressStr,UserDefaultKey_AddressApi);
    UserDefaultsSave(self.portTextField.text,UserDefaultKey_PortApi);
    //to do here
    AppSettings *appSettingShared = [AppSettings sharedInstance];
    [appSettingShared loadServerSetting:self.addressStr Port:self.portTextField.text];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:Default_TipSTR
                                                    message:@"设置服务器地址成功"
                                                   delegate:nil
                                          cancelButtonTitle:Default_OKSTR
                                          otherButtonTitles:nil];
    WeakSelf;
    [alert showWithCompletionHandler:^(NSInteger buttonIndex) {
        [wself.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
}

*/

import UIKit
import Foundation

class BCServerSettingViewController : BCFatherViewController {
    @IBOutlet var portTextField: UITextField!
    @IBOutlet var addressTextField: UITextField!
    @IBOutlet var submitBtn: UIButton!
    
    override func viewDidLoad() {
         super.viewDidLoad()
         title = "服务器设置"
         customBackBarItem()
        
        var addressString = UserDefaults.standard.string(forKey: UserDefaultsKey.serverAddress.rawValue)
        if addressString != nil && addressString?.characters.count != 0 {
            addressTextField.text = UserDefaults.standard.string(forKey: UserDefaultsKey.serverAddress.rawValue)
        }
        
        var portString = UserDefaults.standard.string(forKey: UserDefaultsKey.serverPort.rawValue)
        if portString != nil && portString?.characters.count != 0 {
            portTextField.text = UserDefaults.standard.string(forKey: UserDefaultsKey.serverPort.rawValue)
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        addressTextField.resignFirstResponder()
        portTextField.resignFirstResponder()
    }
    
    override func customBackBarItem () {
        let leftBarBtn = UIBarButtonItem(title: "", style: .plain, target: self,
                                         action: #selector(BCFatherViewController.backToPrevious))
        leftBarBtn.image = UIImage(named: "cancleIcon")
        
        //用于消除左边空隙，要不然按钮顶不到最前面
        let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil,
                                     action: nil)
        spacer.width = 8
        navigationItem.leftBarButtonItems = [spacer, leftBarBtn]
    }
    
    override func backToPrevious() {
        navigationController?.dismiss(animated: true, completion: nil)
    }

    
    @IBAction func submitBtnClick(sender: Any) {
        if addressTextField.isFirstResponder {
            self.addressTextField.resignFirstResponder()
        }
        if portTextField.isFirstResponder {
            self.portTextField.resignFirstResponder()
        }

        if addressTextField.text == nil || addressTextField.text?.characters.count == 0 {
            SVProgressHUD.showError(withStatus: "请输入服务器地址")
            SVProgressHUD.dismiss(withDelay: 2.0)
            return;
        }
        if portTextField.text == nil || portTextField.text?.characters.count == 0 {
            SVProgressHUD.showError(withStatus: "请输入服务器端口")
            SVProgressHUD.dismiss(withDelay: 2.0)
            return;
        }
        if Common.isAllNum(str: portTextField.text!) == false{
            SVProgressHUD.showError(withStatus: "非法端口号,请重新输入")
            SVProgressHUD.dismiss(withDelay: 2.0)
            return;
        }
        if((portTextField.text?.characters.count)! > 5){
            SVProgressHUD.showError(withStatus: "端口号过长")
            SVProgressHUD.dismiss(withDelay: 2.0)
            return;
        }
        
        UserDefaults.standard.setValue(addressTextField.text, forKey:UserDefaultsKey.serverAddress.rawValue)
        UserDefaults.standard.setValue(portTextField.text, forKey:UserDefaultsKey.serverPort.rawValue)
        
        //to do here
        SVProgressHUD.showSuccess(withStatus: "设置成功")
        SVProgressHUD.dismiss(withDelay: 2.0) {
            self.backToPrevious()
        }
    }
}

extension BCServerSettingViewController: UITextFieldDelegate {
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if(textField == addressTextField) {
            addressTextField.resignFirstResponder()
            portTextField.becomeFirstResponder()
        }
        return true;
    }
}
